package jp.co.dailyReport.workItem.sql;

import java.sql.Date;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.workItem.WorkItem;;

/**
 * 作業項目情報を更新するクラスです。
 */
public class WorkItemUpdateExecuter implements Executer {

	private static Logger log = Logger.getLogger(WorkItemUpdateExecuter.class);
	private WorkItem workItem;

	public WorkItemUpdateExecuter(WorkItem workItem) {
		this.workItem = workItem;
	}

	/**
	 * 作業項目IDを基に、作業項目情報を更新します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return null
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		String sql = "update workItem set displayNm=?,deadline=?,unitPrice=? where workItemId=?";
		log.debug("sql: " + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, workItem.getDisplayNm());
			if(workItem.getDeadline()!=null){
				pstmt.setDate(2, new Date(workItem.getDeadline().getTime().getTime()));
			}else{
				pstmt.setDate(2, null);
			}
			pstmt.setInt(3, workItem.getUnitPrice());
			pstmt.setInt(4, workItem.getWorkItemId());
			pstmt.execute();
		} finally {
			if(pstmt != null)
			 pstmt.close();
		}
		return null;
	}

}
